package com.echat.messagesender.sender.config;

import com.weibo.api.motan.config.springsupport.AnnotationBean;
import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
import com.weibo.api.motan.config.springsupport.ProtocolConfigBean;
import com.weibo.api.motan.config.springsupport.RegistryConfigBean;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConditionalOnExpression("${echat.service.motan.enable:false}")
@Log4j2
public class MessageSendMotanConfig {

    @Value("echat.motan.zookeeper.address")
    String registryAddress;

    @Bean
    public AnnotationBean motanAnnotationBean() {
        AnnotationBean motanAnnotationBean = new AnnotationBean();
        motanAnnotationBean.setPackage("com.echat.messagesender.sender.service");
        return motanAnnotationBean;
    }

    @Bean(name = "echat-services")
    public ProtocolConfigBean protocolConfig() {
        ProtocolConfigBean config = new ProtocolConfigBean();
        config.setDefault(true);
        config.setName("motan");
        config.setMaxContentLength(1048576);
        return config;
    }

    @Bean(name = "registryConfig")
    public RegistryConfigBean registryConfig() {
        RegistryConfigBean config = new RegistryConfigBean();
        config.setRegProtocol("zookeeper");
        config.setAddress(registryAddress);
        log.info("motan - RegistryConfigBean regProtocol:" + config.getRegProtocol() +" , address: " +config.getAddress());
        return config;
    }

    @Bean
    public BasicServiceConfigBean baseServiceConfig() {
        BasicServiceConfigBean config = new BasicServiceConfigBean();
        config.setExport("echat-services:8002");
        config.setGroup("group1");
        config.setAccessLog(false);
        config.setShareChannel(true);
        config.setModule("message-sender");
        config.setApplication("echat-services");
        config.setRegistry("registryConfig");
        log.info("motan - BasicServiceConfigBean: " + config.getExport());
        return config;
    }

}
